package com.szholly.utils.freemarker.directive;

import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.springmvc.route.FunctionInfo;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 定义当前日期时间文本标签
 */
@Component("directive_curday")
public class CurDayStringDirective implements TemplateDirectiveModel,
		IDirective {
	
	public static final String PARAM_addE = "addE";

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		
		boolean addE = DirectiveUtils.getBool(PARAM_addE, params);
		SimpleDateFormat df = null;
		if(addE){
			df = new SimpleDateFormat("yyyy年MM月dd日 EEEE");//设置日期格式
		}else{
			df = new SimpleDateFormat("yyyy年MM月dd日");//设置日期格式
		}
		String s = df.format(new Date());
		if (s != null) {
			Writer out = env.getOut();
			out.append(s);
		}
	}

	@Override
	public FunctionInfo getFunctionInfo() {
		String paraInfo = "" + "示例：<@curday  />";
		FunctionInfo info = new FunctionInfo("textcut", 1, "ͨ基础", "当前日期时间文本标签",
				paraInfo);
		return info;
	}
}
